#!/usr/bin/env node

const log = console.log,
      fs = require('fs'),
      com = process.argv[2],
      sr = process.argv[3];

if(com === '-list'){
  log('查看文件夹下的内容！');
  let arrdir = fs.readdirSync('./sysFile');
  class myfile{
    constructor(filename,filesize){
      this.name = filename;
      this.size = filesize;
    }
  }
  let file = [];
  for(let i=0;i<arrdir.length;i++){
    let sta = fs.statSync(`./sysFile/${arrdir[i]}`);
    if(!sta.isDirectory()){
      file[i] = new myfile(arrdir[i],sta.size);
    }
  }
  file = file.filter((item)=>{
    return item!=null;
  });
  file = file.sort((a,b)=>{
    return a.size - b.size;
  });
  log(JSON.stringify(file)); 
}
if(com == '-show'){
  try{
    let data = fs.readFileSync(`./sysFile/${sr}`);
    log(data.toString('utf8'));
  }catch(e){
    log('该文件不存在！');
  }
}
if(com == '-del'){
  let src = `./sysFile/${sr}`;
  function del(src){
    if(fs.statSync(src).isDirectory()){
      log('dir');
    }
    if(fs.statSync(src).isFile()){
      fs.unlinkSync(src);
      log(`删除文件${src}`);
    }
  }
  del(src);
}
